import java.util.Iterator;

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.vocabulary.RDFS;

// example of how to use JENA

public class JenaTest {

	public static void main(String[] args) {

		Model model = ModelFactory.createDefaultModel();

		model
				.read("file:/Users/bermudez/java/TopBraidComposer_old/workspace/obsea/obs.owl");
		// PrintUtil.printOut(model.listStatements());
		Resource cf_sea_water_temperature = model
				.createResource("http://ont.obsea.es/obs.owl#cf_sea_water_temperature");

		StmtIterator iter = model.listStatements(cf_sea_water_temperature,
				RDFS.label, (RDFNode) null);
		Statement sta = iter.nextStatement();
		RDFNode rdfNode = sta.getObject();
		Literal lit = (Literal) rdfNode;
		String lang = lit.getLanguage();
		if (lang.equals("es")) {
			String labelInSpanish = lit.getString();
			System.out.println("labelInSpanish: " + labelInSpanish);
		}

		PrintUtil.printOut(iter);

		System.out.println("using SPARQL");

		String queryString = "SELECT ?p ?o " + "WHERE {<"
				+ obsea.CF_SEA_WATER_TEMPERATURE + "> ?p ?o }";
		com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
		QueryExecution qexec = QueryExecutionFactory.create(query, model);

		try {
			ResultSet results = qexec.execSelect();
			while (results.hasNext()) {
				QuerySolution soln = results.nextSolution();

				Iterator iter2 = soln.varNames();
				while (iter2.hasNext()) {
					RDFNode node = soln.get((String) iter2.next());
					System.out.println(node);

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			qexec.close();
		}

	}

}
